// ClickedPints.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
  'attribute vec4 a_Position;\n' +
  'attribute vec4 a_Color;\n' +
  'uniform mat4 u_matrix;\n' +
  'varying vec4 v_Color;\n' +
  'void main() {\n' +
  '  gl_Position =  u_matrix * a_Position;\n' +
  '  v_Color =  a_Color;\n' +
  '}\n';

// Fragment shader program
var FSHADER_SOURCE =
'precision mediump float;\n' +
  'varying vec4 v_Color;\n' +
  'void main() {\n' +
  '  gl_FragColor = v_Color;\n' +
  '}\n';

var gl;
var verticesColorBuffer;
var u_matrix;
var a_Color;

var curAngle = 0;
var curColor;
var r= 255;
var g=0;
var b=0;

var _r = 0;
var _g = 0;
var _b = 0;

function main() {
  // Retrieve <canvas> element
  var canvas = document.getElementById('webgl');

  // Get the rendering context for WebGL
  gl = getWebGLContext(canvas);
  if (!gl) {
    console.log('Failed to get the rendering context for WebGL');
    return;
  }

  // Initialize shaders
  if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
    console.log('Failed to intialize shaders.');
    return;
  }

  var verticesPositionBuffer = gl.createBuffer();
  var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
  if (a_Position < 0) {
    console.log('Failed to get the storage location of a_Position');
    return;
  }
  var position = new Float32Array([
    0.5,0,-0.5,0,0,0.5
  ]);
  gl.bindBuffer(gl.ARRAY_BUFFER,verticesPositionBuffer);
  gl.bufferData(gl.ARRAY_BUFFER,position,gl.STATIC_DRAW);
  gl.vertexAttribPointer(a_Position,2,gl.FLOAT,false,0,0);
  gl.enableVertexAttribArray(a_Position);

  verticesColorBuffer = gl.createBuffer();
  a_Color = gl.getAttribLocation(gl.program, 'a_Color');
  if (a_Color < 0) {
    console.log('Failed to get the storage location of a_Color');
    return;
  }
  gl.enableVertexAttribArray(a_Color);

  u_matrix = gl.getUniformLocation(gl.program,'u_matrix');

  // Specify the color for clearing <canvas>
  gl.clearColor(0.0, 0.0, 0.0, 1.0);

  // Clear <canvas>
  gl.clear(gl.COLOR_BUFFER_BIT);
  console.log('main')
  draw();
}

function setAngle(angle = 0){
  var cosB = Math.cos(angle);
  var sinB = Math.sin(angle);
  var matrix = new Float32Array([
    cosB,sinB,0.0,0.0,
    -sinB,cosB,0.0,0.0,
    0.0,0.0,1.0,0.0,
    sinB/2,sinB/2,0.0,1.0
  ]);
  gl.uniformMatrix4fv(u_matrix,false,matrix);
}


function setColor(color){
  if(!color){
    color = new Float32Array([
      1,0,0,
      1,0,0,
      1,0,0
    ]);
  }
  gl.bindBuffer(gl.ARRAY_BUFFER,verticesColorBuffer);
  gl.bufferData(gl.ARRAY_BUFFER,color,gl.STATIC_DRAW);
  gl.vertexAttribPointer(a_Color,3,gl.FLOAT,false,0,0);
}

function draw(){
  changePAC();
  setAngle(curAngle);
  setColor(curColor);
  gl.drawArrays(gl.TRIANGLES, 0, 3);
  requestAnimationFrame(()=>{
    draw();
  })
}



function changePAC(){
  curAngle = (curAngle+0.01)%180; 
  if(r>=255&&g<=0){
    _r = -1;
    _g = 1;
    _b = 0;
  }else if(g>=255&&b<=0){
    _r = 0;
    _g = -1;
    _b = 1;
  }else if(b>=255&&r<=0){
      _r = 1;
      _g = 0;
      _b = -1;
  }
  r += _r;
  g += _g;
  b += _b;
    
  curColor = new Float32Array([
    r/255,g/255,b/255,
    r/255,g/255,b/255,
    r/255,g/255,b/255
  ]);
}

  

